document.write("<script src='/js/security/aes.js'></script>");
document.write("<script src='/js/security/pad-zeropadding.js'></script>");
document.write("<script src='/js/security/security.js'></script>");
// 是否开启加密 解密 true 开启，false 不开启
var parameter0 = false;
/*工具类*/
var CoreUtil = (function () {
    var coreUtil = {};

    /* GET */
    coreUtil.sendGet = function (url, params, ft) {
        this.sendAJAX(url, params, ft, "GET")
    }

    /* POST */
    coreUtil.sendPost = function (url, params, ft, cache, async, type) {
        // 请求加密处理
        if (parameter0) {
            CoreUtil.paramsEncrypt(params);
        }

        if (CoreUtil.isEmpty(type)) {
            type = true;
        }

        if (type) {
            this.sendAJAX(url, JSON.stringify(params), ft, "POST", cache, async)
        } else {
            this.sendAJAX_Notoken(url, JSON.stringify(params), ft, "POST",
                cache, async)
        }

    }

    /* PUT */
    coreUtil.sendPut = function (url, params, ft) {
        this.sendAJAX(url, JSON.stringify(params), ft, "PUT")
    }
    /* DELETE */
    coreUtil.sendDelete = function (url, params, ft) {
        this.sendAJAX(url, JSON.stringify(params), ft, "DELETE")
    };

    coreUtil.sendAJAX_Notoken = function (url, params, ft, method, cache, async,
                                          time) {
        if (CoreUtil.isEmpty(time)) {
            time = 30 * 1000;
        }

        $.ajax({
            url: url,
            timeout: time,
            cache: cache,
            async: async,
            data: params,
            type: method,
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            success: function (res) {
                ft(res);
            },
            error: function (XMLHttpRequest, textStatus) {
                if (textStatus === 418) {
                    mui.toast("不支持的客户端", "");
                } else if (textStatus === 419) {
                    mui.toast("未通过人脸验证监控", "");
                } else if (textStatus === 420) {
                    mui.toast("Token为空，请返回首页重新登录再尝试", "");
                } else if (textStatus === 421) {
                    mui.toast("缓存服务器连接丢失", "");
                } else {
                    mui.toast("无法连接服务器，请稍后再试!", "");
                }
            }
        })
    };
    /* ajax */
    coreUtil.sendAJAX = function (url, params, ft, method, cache, async, time) {
        var loadIndex = top.layer.load(0, {
            shade: false
        });
        // add 2021-12-29 13:11:45 其他请求30 * 1000，特殊请求time
        if (CoreUtil.isEmpty(time)) {
            time = 30 * 1000;
        }
        if (CoreUtil.isEmpty(cache)) {
            cache = false;
        }
        if (CoreUtil.isEmpty(async)) {
            async = true;
        }
        $.ajax({
            url: url,
            timeout: time,
            cache: cache,
            async: async,
            data: params,
            type: method,
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            beforeSend: function (request) {
                request.setRequestHeader("authorization", CoreUtil
                    .getData("access_token"));
            },
            success: function (res) {
                top.layer.close(loadIndex);
                // 解密处理
                if (parameter0) {
                    CoreUtil.paramsDecrypt(res);
                }

                if (res.code === 0) {
                    if (ft !== null && ft !== undefined) {
                        ft(res);
                    }
                } else if (res.code === 401001) { // 凭证过期重新登录
                    layer.msg("凭证过期请重新登录", {
                        time: 2000
                    }, function () {
                        top.window.location.href = "/index/login"
                    })
                } else if (res.code === 401008) { // 凭证过期重新登录
                    layer.msg("抱歉！您暂无权限", {
                        time: 2000
                    })
                } else {
                    layer.msg(res.msg);
                    ft(res);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                top.layer.close(loadIndex);
                if (XMLHttpRequest.status === 404) {
                    top.window.location.href = "/index/404";
                } else {
                    layer.msg("服务器好像除了点问题！请稍后试试");
                }
            }
        })
    };

    /* 获取参数值 */
    coreUtil.getParameter = function (params) {

        var mapArr = {
            id: params
        };
        var _parameter = "";
        $.ajax({
            url: "/sys/parameter/info",
            cache: false,
            async: false,
            type: "POST",
            data: JSON.stringify(mapArr),
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    _parameter = res.data.parameter;
                }
            },
            error: function (res) {

            }
        })

        return _parameter;
    }


    /* 获取代码表列表 */
    coreUtil.getDm = function (_url, ft) {
        $.ajax({
            url: _url,
            cache: false,
            async: false,
            type: "POST",
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    ft(res);
                }
            },
            error: function (res) {

            }
        })
    };

    /* 获取代码转名称 */
    coreUtil.getDmToMc = function (_url, params, ft) {
        $.ajax({
            url: _url,
            cache: false,
            async: false,
            data: JSON.stringify(params),
            type: "POST",
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    ft(res);
                }
            },
            error: function (res) {

            }
        })
    };

    /* 存入本地缓存 */
    coreUtil.setData = function (key, value) {
        layui.data('LocalData', {
            key: key,
            value: value
        })
    };
    /* 从本地缓存拿数据 */
    coreUtil.getData = function (key) {
        var localData = layui.data('LocalData');
        return localData[key];
    };

    // 判断字符是否为空的方法
    coreUtil.isEmpty = function (obj) {
        return typeof obj == "undefined" || obj == null || obj === "";
    }

    // Encrypt 加密处理
    coreUtil.paramsEncrypt = function (obj) {
        if (!CoreUtil.isEmpty(obj)) {
            if (!CoreUtil.isEmpty(obj.xm)) {
                obj.xm = Encrypt(obj.xm);
            }

            if (!CoreUtil.isEmpty(obj.zjhm)) {
                obj.zjhm = Encrypt(obj.zjhm);
            }

            if (!CoreUtil.isEmpty(obj.sjhm)) {
                obj.sjhm = Encrypt(obj.sjhm);
            }

            if (!CoreUtil.isEmpty(obj.nsrmc)) {
                obj.nsrmc = Encrypt(obj.nsrmc);
            }

            if (!CoreUtil.isEmpty(obj.nsrsbh)) {
                obj.nsrsbh = Encrypt(obj.nsrsbh);
            }
        }

        return obj;
    }

    // Decrypt 解密处理
    coreUtil.paramsDecrypt = function (obj) {

        if (!CoreUtil.isEmpty(obj)) {
            var _data = obj.data;
            if (!CoreUtil.isEmpty(_data) && _data.length > 0) {
                for (var _i in _data) {

                    if (!CoreUtil.isEmpty(_data[_i].xm)) {
                        _data[_i].xm = Decrypt(_data[_i].xm);
                    }

                    if (!CoreUtil.isEmpty(_data[_i].zjhm)) {
                        _data[_i].zjhm = Decrypt(_data[_i].zjhm);
                    }

                    if (!CoreUtil.isEmpty(_data[_i].sjhm)) {
                        _data[_i].sjhm = Decrypt(_data[_i].sjhm);
                    }

                    if (!CoreUtil.isEmpty(_data[_i].nsrmc)) {
                        _data[_i].nsrmc = Decrypt(_data[_i].nsrmc);
                    }

                    if (!CoreUtil.isEmpty(_data[_i].nsrsbh)) {
                        _data[_i].nsrsbh = Decrypt(_data[_i].nsrsbh);
                    }
                }
            }

            obj.data = _data
        }

        return obj;

    }

    // 字典数据回显
    coreUtil.selectDictLabel = function (datas, value) {
        datas = JSON.parse(datas);
        var label = "";
        $.each(datas, function (index, dict) {
            if (dict.value === ('' + value)) {
                label = dict.label;
                return false;
            }
        });
        // 匹配不到，返回未知
        if (CoreUtil.isEmpty(label)) {
            return "未知";
        }
        return label;
    }
    return coreUtil;
})(CoreUtil, window);
